package df.bean.process;

import df.applet.form.TransDailyFrm;
import df.bean.db.conn.DBConnection;
import df.bean.db.table.Batch;
import df.bean.db.table.StpMethodAllocPayor;
import df.bean.db.table.TRN_Error;
import df.bean.db.table.TrnDaily;
import df.bean.obj.Item.DrMethodAllocation;
import df.bean.obj.doctor.*;
import df.bean.obj.util.DialogBox;
import df.bean.obj.util.JDate;
import df.bean.obj.util.Variables;

public class ProcessTransDaily extends Process {   
    private DrMethodAllocation drMethodAllocation = null;
    private StpMethodAllocPayor drMethodAllocPayor = null;
    String startDate = "";
    String endDate = "";
    private DoctorList drList = null;
    String batch = "";
    
    public ProcessTransDaily(DBConnection conn) {
        super(conn);
        this.setDBConnection(conn);
    }
    public ProcessTransDaily() {
        super();
    }
    public void setBatch(String b){
        this.batch = b;
    }

    public void setDrMedthodAlloction(DrMethodAllocation drMedthodAllocation) {
        this.drMethodAllocation = drMedthodAllocation;
    }
    public void setDrMedthodPayorAlloction(StpMethodAllocPayor drMethodAllocPayor) {
        this.drMethodAllocPayor = drMethodAllocPayor;
    }

    public void run() {
        String lstCode = "";
        try {
            if (working) { 
                values.clear();
                values.add("---- Start ----");
                values.add(JDate.getDate());
                values.add(JDate.getTime());
                this.calculatorFrm.setTableValue(values);
                this.calculatorFrm.setRun();
                // Eash Hospital
//                for (int i=0; i<this.calculatorFrm.getLstHospitalCode().getModel().getSize(); i++) {
                    this.calculatorFrm.getLstHospitalCode().setSelectedIndex(0);
                    this.calculatorFrm.setHospitalCode(this.calculatorFrm.getLstHospitalCode().getSelectedValue().toString());
                    this.calculatorFrm.addDoctorGroupToList(Variables.getHospitalCode());

                    // create batch // don't remove because we need to use static variable such as YYYY and MM
                    Batch batch = new Batch(Variables.getHospitalCode(), this.getDBConnection());
                    this.drList = new DoctorList(Variables.getHospitalCode(), this.getDBConnection());
                    drMethodAllocation = new DrMethodAllocation(this.getDBConnection(), Variables.getHospitalCode());
                    drList.newAllDoctor(Variables.getHospitalCode());
                    startDate = ((TransDailyFrm)this.calculatorFrm).getStartComputeDate();
                    endDate = ((TransDailyFrm)this.calculatorFrm).getEndComputeDate();
                    
                    // Eash Doctor
                    for (int j=0; j<this.calculatorFrm.getLstDoctorGroupCode().getModel().getSize(); j++) {
                        this.calculatorFrm.getLstDoctorGroupCode().setSelectedIndex(j);
                        lstCode = this.calculatorFrm.getLstDoctorGroupCode().getSelectedValue().toString();
                        this.calculatorFrm.setCount((j+1) + " : " + this.calculatorFrm.getLstDoctorGroupCode().getModel().getSize());

                        int numAffRec = 0;
                        setDoctorList(drList);
                        setDrMedthodAlloction(drMethodAllocation);
                        if (Calculate(startDate, endDate, Variables.getHospitalCode(), lstCode)) {
                            numAffRec = 1; 
                        }else {
                            numAffRec = 0;
                        }
                        
                        if (!running) break;
                        if (!working) break;
                    } 
                    batch = null;
//                }

                working = false;
//                    running = false;    // add for exit loop
                this.calculatorFrm.setStop();
                values.clear();
                values.add("==== Stop ====");
                values.add(JDate.getDate());
                values.add(JDate.getTime());
                this.calculatorFrm.setTableValue(values);
                values = null; 
                this.calculatorFrm.stopProcess();
            }
        } catch (Exception e) {
            // TODO
//                DialogBox.ShowError("Error in Process.Run() \n" + lstCode + "\n" + e.getMessage());
            System.out.print(e.getMessage());
            this.calculatorFrm.stopProcess();
//                break;
        } finally {        }
                 
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            // TODO
//                DialogBox.ShowError("Error in Process.Run().sleep \n" + e.getMessage());
//                break;
            this.calculatorFrm.stopProcess();
        }
//        }

        DialogBox.ShowInfo("     Completed !!!!   ");
    }
    
    // used 
    public boolean Calculate(String startDate, String endDate, String hospitalCode, String invoiceNo ) {
        boolean ret = false;
        String doctorCode = "";
        CareProvider careProvider = null;
        int iCount = 0;
        if (drMethodAllocation == null) { drMethodAllocation = new DrMethodAllocation(this.getDBConnection(), Variables.getHospitalCode()); }
        TrnDaily tDaily = new TrnDaily(this.getDBConnection());
            try {
                ret = true;
                String sql = TrnDaily.getSQL_TRN_DAILY(startDate, endDate, hospitalCode, invoiceNo);
                tDaily.OpenResultSet(sql);
//                this.getDBConnection().beginTrans();
                
                while (tDaily.MoveNext()) {
                    values.clear();
                    values.add(JDate.getTime());
                    values.add(tDaily.getInvoiceNo());
                    values.add(tDaily.getInvoiceDate());
                    values.add(tDaily.getLineNo());
                    values.add(tDaily.getDoctorCode());
                    values.add(tDaily.getOrderItemCode());
                    
                    doctorCode = tDaily.getDoctorCode();
                    careProvider = this.getDoctorList().getDoctor(doctorCode);
                    if (careProvider != null) {
                        careProvider.setTrnDaily(tDaily);
                        careProvider.setDrMethodAllocation(this.drMethodAllocation);
                        if (careProvider.computeTransDaily()) {   
                            iCount++;
                            values.add("Ok");
                            //ret = true;    
                        } 
                        else {  
                            values.add(careProvider.getMessage());
                            ret = false;
//                            break;   
                        }
                    } else {
                        values.add("Doctor Code : " + doctorCode + " is not found.");
                        TRN_Error.writeErrorLog(this.getDBConnection().getConnection(), 
                                TRN_Error.PROCESS_DAILY, doctorCode + " is not found.", 
                                this.batch, TRN_Error.ERROR_TYPE_NO_DOCTOR_CODE);
                        ret = false;
//                        break;
                    }
                    if (this.calculatorFrm != null) this.calculatorFrm.setTableValue(values);
                }
//                if (ret) { this.getDBConnection().commitTrans(); }
//                if (!ret) { this.getDBConnection().rollBackTrans(); }
//                if (ret) { ret = tDaily.updateCompute(startDate, endDate, hospitalCode, invoiceNo, lineNo); }
            } catch (Exception e) {
                // TODO
                e.printStackTrace();
                TRN_Error.writeErrorLog(this.getConn().getConnection(), TRN_Error.PROCESS_DAILY, "", e.getMessage());
                ret=false;
            } finally {
               //Clean up resources, close the connection.
                try {
                    tDaily.setStatement(null);
                    tDaily.setResultSet(null);
                    tDaily = null;
                } catch (Exception ex) { 
                    ex.printStackTrace();  
                    TRN_Error.writeErrorLog(this.getConn().getConnection(), TRN_Error.PROCESS_DAILY, "", ex.getMessage());
                    ret = false; 
                } 
            }

        return ret;
    } 
    
    public boolean Calculate(String startDate, String endDate, String hospitalCode, String invoiceNo, String lineNo ) {
        boolean ret = false;
        String doctorCode = "";
        CareProvider careProvider = null;
        int iCount = 0;
        if (drMethodAllocation == null) { drMethodAllocation = new DrMethodAllocation(this.getDBConnection(), Variables.getHospitalCode()); }
        TrnDaily tDaily = new TrnDaily(this.getDBConnection());
            try {
                ret = true;
                String sql = TrnDaily.getSQL_TRN_DAILY(startDate, endDate, hospitalCode, invoiceNo, lineNo);
                tDaily.OpenResultSet(sql);

                while (tDaily.MoveNext()) {
                    doctorCode = tDaily.getDoctorCode();
                    careProvider = this.getDoctorList().getDoctor(doctorCode);
                    if (careProvider != null) {
                        careProvider.setTrnDaily(tDaily);
                        careProvider.setDrMethodAllocation(this.drMethodAllocation);
                        if (careProvider.computeTransDaily()) {   
                            iCount++;
                            ret = true;    } 
                        else {  
                            ret = false;
                            break;   
                        }
                    }
                }
                if (ret) { ret = tDaily.updateCompute(startDate, endDate, hospitalCode, invoiceNo, lineNo); }
            } catch (Exception e) {
                // TODO
                e.printStackTrace();
                TRN_Error.writeErrorLog(this.getConn().getConnection(), TRN_Error.PROCESS_DAILY, "", e.getMessage());
                ret=false;
            } finally {
               //Clean up resources, close the connection.
                try {
                    tDaily = null;
                } catch (Exception ex) { 
                    ex.printStackTrace();  
                    TRN_Error.writeErrorLog(this.getConn().getConnection(), TRN_Error.PROCESS_DAILY, "", ex.getMessage());
                    ret = false; 
                } 
            }

        return ret;
    } 
    
}
